#include <QtNetwork>
#include <QDebug>
#include "ClientSocket.h"
#include "ServerThreads.h"



ClientSocketThread::ClientSocketThread(int socketDescriptor, QObject *parent)
    : QThread(parent), socketDescriptor(socketDescriptor)
{
    pTcpSocket = NULL;
}
ClientSocketThread::~ClientSocketThread()
{
    if(pTcpSocket)
    {
        delete pTcpSocket;
        //pTcpSocket->deleteLater();
        //pTcpSocket->waitForDisconnected();
        pTcpSocket = NULL;
    }
    qDebug()<<"thread析构";
}
void ClientSocketThread::run()
{
    if(pTcpSocket == NULL)
    {
        pTcpSocket = new TcpSocket;//(this);
        //pTcpSocket->moveToThread(this);
        if (!pTcpSocket->setSocketDescriptor(socketDescriptor))
        {
            //qDebug()<<(int)pTcpSocket->error();
            qDebug()<<pTcpSocket->errorString();
            return;
        }


        connect(pTcpSocket, &TcpSocket::s_disconnected, this, [=](TcpSocket *)
        {
            emit socketDisconnected(this);
        });






        connect(pTcpSocket, SIGNAL(s_hostFound(TcpSocket *)),
                  this, SIGNAL(hostFound(TcpSocket *)));
        //连接函数
        connect(pTcpSocket, SIGNAL(s_connected(TcpSocket *)),
                  this, SIGNAL(connected(TcpSocket *)));
        //断开
        connect(pTcpSocket, SIGNAL(s_disconnected(TcpSocket *)),
                  this, SIGNAL(disconnected(TcpSocket *)));

        connect(pTcpSocket, SIGNAL(s_stateChanged(qint16, TcpSocket *)),
                  this, SIGNAL(stateChanged(qint16, TcpSocket *)),Qt::QueuedConnection);
        //错误函数
        connect(pTcpSocket, SIGNAL(s_error(qint16, TcpSocket *)),
                  this, SIGNAL(error(qint16, TcpSocket *)));
        //有数据
        connect(pTcpSocket, SIGNAL(s_readyRead(TcpSocket *)),
                  this, SIGNAL(readyRead(TcpSocket *)));
        connect(pTcpSocket, SIGNAL(s_bytesWritten(qint64, TcpSocket *)),
                  this, SIGNAL(bytesWritten(qint64, TcpSocket *)));
        connect(pTcpSocket, SIGNAL(s_aboutToClose(TcpSocket *)),
                  this, SIGNAL(aboutToClose(TcpSocket *)));
        connect(pTcpSocket, SIGNAL(s_readChannelFinished(TcpSocket *)),
                  this, SIGNAL(readChannelFinished(TcpSocket *)));

    }

    exec();
}
























TcpServerThread::TcpServerThread(QObject * parent):QTcpServer(parent)
{
}
TcpServerThread::~TcpServerThread()
{
    ClientSocketThread *pThread;
    while(!clientList.isEmpty())
    {
        pThread = clientList.takeFirst();
        pThread->quit();
        pThread->wait();
        pThread->deleteLater();//delete
    }
    close();
}

void TcpServerThread::Remove(ClientSocketThread *pThread)
{
    if(pThread != NULL)
    {
        if(clientList.removeOne(pThread))
        {
            pThread->quit();
            pThread->wait();
            pThread->deleteLater();
        }
    }
}

ClientSocketThread* TcpServerThread::createSocketThread(qintptr handle)
{
    return new ClientSocketThread(handle, this);
}

void TcpServerThread::incomingConnection(qintptr handle)
{
    ClientSocketThread * thread = createSocketThread(handle);
    clientList.append(thread);

    connect(thread,SIGNAL(socketDisconnected(ClientSocketThread*)), this, SIGNAL(socketDisconnected(ClientSocketThread*)));
    thread->start();



    connect(thread, SIGNAL(hostFound(TcpSocket *)),
              this, SIGNAL(hostFound(TcpSocket *)));
    //连接函数
    connect(thread, SIGNAL(connected(TcpSocket *)),
              this, SIGNAL(connected(TcpSocket *)));
    //断开
    connect(thread, SIGNAL(disconnected(TcpSocket *)),
              this, SIGNAL(disconnected(TcpSocket *)));

    connect(thread, SIGNAL(stateChanged(qint16, TcpSocket *)),
              this, SIGNAL(stateChanged(qint16, TcpSocket *)));
    //错误函数
    connect(thread, SIGNAL(error(qint16, TcpSocket *)),
              this, SIGNAL(error(qint16, TcpSocket *)));
    //有数据
    connect(thread, SIGNAL(readyRead(TcpSocket *)),
              this, SIGNAL(readyRead(TcpSocket *)));
    connect(thread, SIGNAL(bytesWritten(qint64, TcpSocket *)),
              this, SIGNAL(bytesWritten(qint64, TcpSocket *)));
    connect(thread, SIGNAL(aboutToClose(TcpSocket *)),
              this, SIGNAL(aboutToClose(TcpSocket *)));
    connect(thread, SIGNAL(readChannelFinished(TcpSocket *)),
              this, SIGNAL(readChannelFinished(TcpSocket *)));

}
